package com.qingcheng.consumer;

import com.alibaba.fastjson.JSON;
import com.qingcheng.pojo.order.OrderItem;
import com.qingcheng.service.goods.StockBackService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @Title: StockBackConsumer
 * @Description:
 * @Auther:
 * @Version: 1.0
 * @create 2021/10/15 14:21
 */
public class StockBackConsumer implements MessageListener {

    @Autowired
    private StockBackService stockBackService;
    /**
     * 监听队列的消息
     * @param message
     */
    public void onMessage(Message message) {
        /**
         * 怎么来处理这个消息
         */
        try {
            String jsonString = new String(message.getBody());
            List<OrderItem> orderItems = JSON.parseArray(jsonString, OrderItem.class);
            //讲数据保存到 库存回滚表
            stockBackService.insert(orderItems);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
